<?php
/**************************************************************************
	ESAM (EVE to SMF API Module)
	Copyright (C) 2009  Clinton Weiss

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

include_once "EsamQuery.php";
include_once "EsamEveApi.php";

class EsamAlliance
{
  private $cache = array();

	function getAllianceName($allianceID)
	{
    if (!empty($this->cache["$allianceID"]))
    {
      return $this->cache["$allianceID"];
    }
		$ea = new EsamEveApi();
		$allianceList = $ea->getAllianceList();
		$allianceXml = new SimpleXmlElement($allianceList);
		foreach($allianceXml->result->rowset->row as $allianceRow)
		{
			if ( (string) $allianceRow['allianceID'] == $allianceID )
			{
        $this->cached["$allianceID"] = $allianceRow['name'];
				return $allianceRow['name'];
			}
		}

		return 0;
	}

	function getAllianceId($allianceName)
	{
    if (!empty($this->cache["$allianceName"]))
    {
      return $this->cache["$allianceName"];
    }
		$ea = new EsamEveApi();
		$allianceList = $ea->getAllianceList();
		$allianceXml = new SimpleXmlElement($allianceList);
		foreach($allianceXml->result->rowset->row as $allianceRow)
		{
			if ( (string) $allianceRow['name'] == $allianceName )
			{
        $this->cached["$allianceName"] = $allianceRow['name'];
				return $allianceRow['allianceID'];
			}
		}

		return 0;
	}
	
	function getCorporationName($corporation_id)
	{
    if (!empty($this->cache["$corporation_id"]))
    {
      return $this->cache["$corporation_id"];
    }
		$eve_api = new EsamEveApi();
		$eve_api->setCredentials(null, null, null, $corporation_id);
		$corp_info = $eve_api->getCorporationInfo();
		$corp_xml = new SimpleXMLElement($corp_info);
    $this->cached["$corporation_id"] = (string) $corp_xml->result->corporationName;
		return (string) $corp_xml->result->corporationName;
	}

	function getCharacterName($character_id)
	{
    if (!empty($this->cache["$character_id"]))
    {
      return $this->cache["$character_id"];
    }
		$eve_api = new EsamEveApi();
		$charInfo = $eve_api->getCharacterName($character_id);
		$xml = new SimpleXMLElement($charInfo);
    $this->cached["$character_id"] = (string) $xml->result->rowset->row['name'];
		return (string) $xml->result->rowset->row['name'];
	}
}
?>
